<?php
	/**
	* @File name:	admin.php
	* @Desription:	description
	* @Author:	 	jinxin<alucard.jx@gmail.com>   
	* @Date:	 	Sat Oct 08 18:11:28 CST 2011
	* @Copyright 	Copytright (c) jinxin<alucard.jx@gmail.com>
	* @version	 	$Id $
	*/
	
	class admin_poll_model_admin extends model {
		public $tablepre = 'poll_';
		public function __construct(){
			parent::__construct();
		}
		
		public function addpoll($data){
			return $this->db->insertRow($this->tablepre.'body',$data);
		}
		
		public function addKeys($data){
			$this->db->insertRows($this->tablepre.'key',$data);
		}
		public function addQes($data){
			return $this->db->insertRow($this->tablepre.'key',$data);
		}
		
		public function getList($page,$pagesize,$condision=''){
			$cds = ($condision == '' ? 'ORDER BY id DESC' : $condision);
			return $this->selectPage("SELECT * FROM {$this->tablepre}body {$cds}",$page,$pagesize);
		}
		
		public function addFields($data){
			$this->db->insertRows($this->tablepre.'fields',$data);
		}
		
		public function getPollBody($pid){
			return $this->db->fetchRow("SELECT * FROM {$this->tablepre}body WHERE id ={$pid}");
		}
		
		public function getPollKeys($pid){
			return $this->db->fetchAll("SELECT * FROM {$this->tablepre}key WHERE pid={$pid}");
		}
		
		public function getPollFields($pid){
			return $this->db->fetchAll("SELECT * FROM {$this->tablepre}fields WHERE pid={$pid}");
		}
		
		public function getPoll($pid){
			$data['body'] = $this->getPollBody($pid);
			$data['keys'] = $data['body']['type'] <4 ? $this->getPollKeys($pid) : $this->formatKeys($pid);
			$data['fields'] =  $this->getPollFields($pid);
			return $data;
		}
		public function formatKeys($pid){
			$tmp = $this->getPollKeys($pid);
			if (!empty($tmp)) {
				foreach ($tmp as $key=>$row){
					if ($row['tid'] == 0) {
						$ret[$row['id']] = $row;
					}else {
						$ret[$row['tid']]['key'][$row['id']] = $row;
					}
				}
				return $ret;
			}
		}
		
		public function delPoll($id) {
			$ids = is_array($id) ? implode(',',$id) : (int)$id;
			$this->db->query("DELETE FROM {$this->tablepre}body WHERE id IN ({$ids})");
			$this->db->query("DELETE FROM {$this->tablepre}key WHERE pid IN ({$ids})");
		}
		
		public function updatePollbody($pid,$data) {
			
		}
	}